package gatperdut.chato.core.interpreter.actions;

import gatperdut.chato.constants.CSConstantsCommandResult.CommandExecutionResult;
import gatperdut.chato.core.characters.BaseController;
import gatperdut.chato.core.characters.BotController;
import gatperdut.chato.core.entities.PlayerController;
import gatperdut.chato.core.interpreter.actions.generic.Action;
import gatperdut.chato.core.interpreter.grammar.comminfo.QuitInfo;

public class QuitAction extends Action {

    public QuitAction( QuitInfo info, BaseController controller ) {
        super( info, controller );
    }

    @Override
    public CommandExecutionResult perform() {
        if ( getEntityController().getCombatController().isInCombat() ) {
            getEntityController().tx( "You cannot log out while in combat." );
            return CommandExecutionResult.OK;
        }
        if ( getEntityController() instanceof BotController ) {
            getEntityController().tx( "Bots are not allowed to log out." );
            return CommandExecutionResult.OK;
        }

        checkForRunningCraft();

        getEntityController().getEchoes().txLeaveArea();
        ( (PlayerController) getEntityController() ).logout();
        getEntityController().getChatoServer().getDatasource().save();
        return CommandExecutionResult.QUIT_TO_MAIN_MENU;
    }

    @Override
    public QuitInfo getInfo() {
        return (QuitInfo) super.getInfo();
    }

    @Override
    public boolean interruptsCrafting() {
        return true;
    }

}
